package com.bjsxt.shop.back.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.controller.BaseController;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.goods.pojo.AGoodsBrand;
import com.bjsxt.shop.goods.pojo.AGoodsCate;
import com.bjsxt.shop.goods.service.IGoodsService;
import com.bjsxt.shop.users.pojo.AAdmins;
/**
 * 商品的Controller
 * @author haihai
 *
 */
@Controller
@RequestMapping("/back/goods")
public class GoodsBackController extends BaseController
{
	@Resource
	private IGoodsService goodsService;
	/*--------- 品牌操作开始 ------------*/
	/**
	 * 品牌列表
	 * @return
	 */
	@RequestMapping("/brandList")
	public String brandList(HttpServletRequest request)
	{
		
		ConstatFinalUtil.LOGGER.info("---品牌列表：brandList---");
		
		/* 接收搜索条件 */
		String keyword=request.getParameter("keyword");
		String status=request.getParameter("status");
		String st=request.getParameter("st");
		String ed=request.getParameter("ed");
		/* 处理搜索条件 */
		if(keyword ==null)
		{
			keyword="";
		}
		/*去除空格*/
		keyword=keyword.trim();
		if(status ==null)
		{
			status="";
		}
		if(st ==null)
		{
			st="";
		}
		if(ed ==null)
		{
			ed="";
		}
		
		Date stDate=null;
		Date edDate=null;
		if(!"".equalsIgnoreCase(st)&&!"".equalsIgnoreCase(ed))
		{
			stDate=this.dateFormatUtil.parse(st);
			edDate=this.dateFormatUtil.parse(ed);
		}
		Map<String, Object> condMap=new HashMap<String,Object>();
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		/*分页*/
		PageInfoUtil pageInfoUtil=this.proccedPageInfo(request);
		List<AGoodsBrand>goodsBrandList=this.goodsService.findCondListGoodsBrandService(pageInfoUtil, condMap);
		
		/*存储结果*/
		request.setAttribute("goodsBrandList", goodsBrandList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		request.setAttribute("websitePath", ConstatFinalUtil.BUNDLE.getString("website.path"));
		
		/* 存储条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		return "/back/goods/brandList";
	}
	
	/**
	 * 品牌添加
	 * @return
	 */
	@RequestMapping("/brandInsert")
	public String brandIsert()
	{
		
		ConstatFinalUtil.LOGGER.info("---品牌添加：brandIsert---");
		return "/back/goods/brandInsert";
	}
	
	/**
	 * 品牌添加提交
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/brandInsertSubmit")
	public String brandIsertSubmit(HttpServletRequest request,AGoodsBrand goodsBrand,MultipartFile file) throws IOException
	{
		
		ConstatFinalUtil.LOGGER.info("---品牌添加提交：brandInsertSubmit---");
		HttpSession session=request.getSession();
		AAdmins admins=(AAdmins) session.getAttribute("admins");
		
		String pubTimeStr=request.getParameter("pubTimeStr");
		goodsBrand.setPubTime(this.dateFormatUtil.parse(pubTimeStr));
		 
		goodsBrand.setAdminsId(admins.getId());
		goodsBrand.setCreateTime(new Date());
		goodsBrand.setUpdateTime(new Date());
		
		Map<String,Object>paramsMap=new HashMap<String,Object>();
		if(file !=null)
		{
			paramsMap.put("oriFileName", file.getOriginalFilename());
			paramsMap.put("inputStream", file.getInputStream());
		}
		JSONObject resDBJSON=this.goodsService.saveOneGoodsBrandService(goodsBrand,paramsMap);
		this.info=resDBJSON.getString("info");
		request.setAttribute("info", info);
		return this.brandIsert();
	}
	
	
	/**
	 * 品牌更新
	 * @return
	 */
	@RequestMapping("/brandUpdate")
	public String brandUpdate(HttpServletRequest request , String bid)
	{
		ConstatFinalUtil.LOGGER.info("---- brandUpdate ----");
		/* 按照Id查询 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", bid);
		/* 查询数据库 */
		AGoodsBrand goodsBrand = this.goodsService.findOneGoodsBrandService(condMap);
		/* 存储request */
		request.setAttribute("goodsBrand", goodsBrand);
		request.setAttribute("websitePath", ConstatFinalUtil.BUNDLE.getString("website.path"));
		return "/back/goods/brandUpdate";
	}
	
	/**
	 * 品牌更新提交
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/brandUpdateSubmit")
	//在更新提交时不建议使用对象属性驱动，因为在表里有10个值，而页面只有5个值体现，最后5个字段为默认值，在xml映射文件中需要一一判断，而且有多表的操作
	public String brandUpdateSubmit(HttpServletRequest request,String bid ,MultipartFile file) throws IOException
	{
		ConstatFinalUtil.LOGGER.info("---- brandUpdateSubmit ----");
		/* 按照Id查询 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", bid);
		/* 查询数据库 */
		AGoodsBrand goodsBrand = this.goodsService.findOneGoodsBrandService(condMap);
		/* 存储request */
		request.setAttribute("goodsBrand", goodsBrand);
		
		String pubTimeStr = request.getParameter("pubTimeStr");
		goodsBrand.setPubTime(this.dateFormatUtil.parse(pubTimeStr));
		//获取网页上的参数值
		String name = request.getParameter("name");
		String website = request.getParameter("website");
		String content = request.getParameter("content");
		String status = request.getParameter("status");
		
		goodsBrand.setName(name);
		goodsBrand.setWebsite(website);
		goodsBrand.setContent(content);
		goodsBrand.setStatus(Byte.valueOf(status));
		
		goodsBrand.setUpdateTime(new Date());
		
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		if(file != null)
		{
			paramsMap.put("oriFileName", file.getOriginalFilename());
			paramsMap.put("inputStream", file.getInputStream());
		}
		JSONObject resDBJSON = this.goodsService.updateOneGoodsBrandService(goodsBrand,paramsMap);
		this.info = resDBJSON.getString("info");
		request.setAttribute("info", info);
		return this.brandUpdate(request,bid);
	}
	/*--------- 品牌操作结束------------*/
	
	/*----------分类操作开始---------*/
	/**
	 * 分类列表
	 * @return
	 */
	@RequestMapping("/cateList")
	public String cateList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("---- cateList ----");
		/* 接收搜索条件 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		/* 处理搜索条件 */
		if(keyword == null)
		{
			keyword = ""; 
		}
		keyword = keyword.trim() ; 
		if(status == null)
		{
			status = ""; 
		}
		if(st == null)
		{
			st = ""; 
		}
		if(ed == null)
		{
			ed = ""; 
		}
		
		Date stDate = null ; 
		Date edDate = null ; 
		if(!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.parse(st);
			edDate = this.dateFormatUtil.parse(ed);
		}
		
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("keyword", keyword);
		if(!"".equalsIgnoreCase(status))
		{
			condMap.put("status", status);
		}
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);
		/* 分页 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		List<AGoodsCate> cateList = this.goodsService.findCondListGoodsCateService(pageInfoUtil, condMap);
		
		/* 存储结果 */
		request.setAttribute("cateList", cateList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		
		/* 存储条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		return "/back/goods/cateList" ; 
	}
	/**
	 * 分类添加
	 * @return
	 */
	@RequestMapping("/cateInsert")
	public String cateInsert()
	{
		ConstatFinalUtil.LOGGER.info("---- cateInsert ----");
		return "/back/goods/cateInsert";
	}
	
	/**
	 * 分类添加提交
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/cateInsertSubmit")
	public String cateInsertSubmit(HttpServletRequest request,AGoodsCate goodsCate,MultipartFile file) throws IOException
	{
		ConstatFinalUtil.LOGGER.info("---- cateInsertSubmit ----");
		HttpSession session = request.getSession() ; 
		AAdmins admins = (AAdmins) session.getAttribute("admins");
		
		String pubTimeStr = request.getParameter("pubTimeStr");
		goodsCate.setPubTime(this.dateFormatUtil.parse(pubTimeStr));
		
		goodsCate.setAdminsId(admins.getId());
		goodsCate.setCreateTime(new Date());
		goodsCate.setUpdateTime(new Date());
		
		JSONObject resDBJSON = this.goodsService.saveOneGoodsCateService(goodsCate);
		this.info = resDBJSON.getString("info");
		request.setAttribute("info", info);
		return this.cateInsert();
	}
	
}
